package com.qf.flash.ui.circle;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.model.MethodDetailsBean;
import com.qf.flash.mvp.contract.MethodDetailsContract;
import com.qf.flash.mvp.presenter.MethodDetailsPresenter;
import com.qf.flash.utils.ImageLoader;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.base.WebViewFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 攻略详情---
 */
public class MethodDetailsActivity extends BaseActivity implements MethodDetailsContract.View {
    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    MethodDetailsPresenter detailsPresenter;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_author)
    TextView tvAuthor;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.iv_pic)
    ImageView ivPic;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_method_details;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("剧本杀攻略");
        String id = getIntent().getStringExtra("id");
        detailsPresenter.getMethodDetails(id);
    }

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, MethodDetailsActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    public int getFragmentGroupId() {
        return R.id.fl_container;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }


    @Override
    public void setDetails(MethodDetailsBean bean) {
        title.setText(bean.getTitle());
        tvAuthor.setText(bean.getAuthor());
        time.setText(bean.getShowTime());
        ImageLoader.load(MethodDetailsActivity.this,bean.getImage(),ivPic);
        if (!TextUtils.isEmpty(bean.getContent()))
            mFragment = WebViewFragment.initWebViewFragment(this, bean.getContent(),false);


    }
    private WebViewFragment mFragment;

    @OnClick(R.id.tv_top_back)
    public void onViewClicked()
    {
        if (mFragment != null)
        {
            mFragment.onKeyDown(KeyEvent.KEYCODE_BACK);
        }
        else
        {
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (mFragment != null)
            return mFragment.onKeyDown(keyCode);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void initInject() {
        super.initInject();
        detailsPresenter = new MethodDetailsPresenter();
        initPresenters(detailsPresenter);
    }
}
